//
//  ZCMVAllTableViewCell.m
//  VoiceMusic
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCMVAllTableViewCell.h"
#import <Masonry.h>
#import "ZCMVAllModel.h"
#import "ZCMVAllCollectionViewCell.h"
@implementation ZCMVAllTableViewCell

- (void)dealloc
{
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 5;
    _layout.itemSize = CGSizeMake((rect.size.width - 20) / 4, 40);
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //竖向之间的距离
    _layout.minimumLineSpacing = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[ZCMVAllCollectionViewCell class] forCellWithReuseIdentifier:@"reuse1"];
    [self.contentView addSubview:self.collectionView];
}

- (void)layoutSubviews
{
    [super superview];
    CGRect rect = [[UIScreen mainScreen] bounds];
//    self.layout.itemSize = CGSizeMake(((rect.size.width - 20) / 4) / 375.0 * self.contentView.frame.size.width, 40 / 667.0 * self.contentView.frame.size.height);
    self.layout.itemSize = CGSizeMake((rect.size.width - 20) / 4, 40);
    if (self.allArray.count / 3 != 1) {
        self.collectionView.frame = CGRectMake(10, 10, rect.size.width - 20, 50 * self.allArray.count / 3 );
    }else {
        self.collectionView.frame = CGRectMake(10, 10, rect.size.width - 20, 50 * self.allArray.count / 3  + 40);
    }
//    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.contentView).offset(0);
//    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZCMVAllCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    ZCMVAllModel *model = self.allArray[indexPath.row];
    cell.tagLable.text = model.name;
    return cell;
}
// 通知中心
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZCMVAllModel *model = self.allArray[indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:model.ID, @"key",nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"return" object:nil userInfo:dic];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
